package com.ljm.controller;/**
 * @author： li
 * @date： 2022-05-01 13:23
 * @version 1.0
 */

import com.ljm.bin.imageUrl;
import com.ljm.util.ImageUrlUtil;
import com.ljm.util.ImageUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author： ljm
 * @date： 2022-05-01 13:23
 * @version 1.0
 */
@WebServlet("/getWallPaper")
public class dailyWallPaperServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/json; charset=utf-8");
        String class_k = req.getParameter("class");
        int k=Integer.parseInt(class_k);
        PrintWriter out =res.getWriter();
        JSONObject jo = new JSONObject();
        JSONArray ja = new JSONArray();
        List<imageUrl> urls= null;
        try {
            urls = ImageUrlUtil.result(k);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int t=0;
        for(imageUrl i:urls){
            jo.put("title",i.getTitle());
            jo.put("url",i.getUrl());
            jo.put("date",i.getDate());
            ja.add(jo);
        }
        out.println(ja);
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
